package h0710;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class MemoryAllocation {
   static class MemoryBlock{
       int offset;
       int size;
       public MemoryBlock(int offset,int size){
           this.offset=offset;
           this.size=size;
       }
   }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int requestSize= sc.nextInt();
        List<MemoryBlock> allocatedBlocks=new ArrayList<>();

        while (sc.hasNextInt()){
            int offset=sc.nextInt();
            int size = sc.nextInt();
            allocatedBlocks.add(new MemoryBlock(offset,size));
        }
        //按照起始位置排序
        allocatedBlocks.sort((a, b) -> Integer.compare(a.offset,b.offset));

        int previousEnd=0;
        int bestOffset=-1;
        int minWastedSpace=Integer.MAX_VALUE;

        for (MemoryBlock block : allocatedBlocks) {
            int freeSpaceStart=previousEnd;//空闲内存块的起始位置
            int freeSpaceSize= block.offset-previousEnd; //空闲内存块的大小

            if (freeSpaceSize>=requestSize&&freeSpaceSize<minWastedSpace){
                bestOffset=freeSpaceStart; //如果满足要求
                minWastedSpace=freeSpaceSize;
            }
            previousEnd= block.offset+ block.size;
        }
        //在最后一次分配内存后检查是否合格
        int freeSpaceStart=previousEnd;
        int freeSpaceSize=100-previousEnd;
        if (freeSpaceSize>=requestSize&&freeSpaceSize<minWastedSpace){
            bestOffset=freeSpaceStart;
        }
        System.out.println(bestOffset);
    }
}
